﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.DAL;
using VgoCardChange.Models.Objects;
using System.Globalization;

namespace VgoCardChange.Models
{
    public class CardBillModels
    {
       ConnectDB objData ;
        //set insert ID TrangLog off
        public bool CheckTranLogID(string TransID) 
        { 
            bool ishaveID = false;
            objData = new ConnectDB();
            string strSQL = "";
            try 
            {
                strSQL = "Select TransID from TblTransactionLog where TransID='" + TransID + "'";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {
                    string ID = objData.dataRead["TransID"].ToString();
                    if (TransID == ID.Trim())
                    {
                        ishaveID = true;
                        return ishaveID;
                    }
                    else
                        ishaveID = false;
                }
                else
                    ishaveID = false;
            }catch(Exception ex)
            {
                Console.WriteLine(ex);
            }finally
            {
                objData.CloseData();
                objData = null;
            }
            return ishaveID;
        }
       //Function Savelog CardCharging
        #region SaveLog
        public void SaveLog(TransactionLog tranLog)
        {
            objData = new ConnectDB();
            string strSQLSaveLog = "";
            try
            {
                //Save log query
                strSQLSaveLog = "sp_SaveCardChargingLog @TransID='" + tranLog.TransID + "'"
                                                     + ",@UserID =" + tranLog.UserID + ""
                                                     + ",@TransDate ='" + tranLog.TransDate + "'"
                                                     + ",@IpAddress ='" + tranLog.IpAddress + "'"
                                                     + ",@UserAgent=N'" + tranLog.UserAgent +"'"
                                                     + ",@TransStatus =N'" + tranLog.TransStatus +"'"
                                                     + ",@CardSupplierID ='" + tranLog.CardSupplierID +"'"
                                                     + ",@CardNumber = '" + tranLog.CardNumber +"'"
                                                     + ",@SeriNumber = '" + tranLog.SeriNumber +"'"
                                                     + ",@Money =" + tranLog.Money +""
                                                     + ",@Bonus =" + tranLog.Bonus + ""
                                                     + ",@Description =N'" + tranLog.Description + "'"
                                                     + ",@GameID =" +tranLog.GameID + "";
                //lưu lịch sử xuống cơ sở dữ liệu
                objData.ExeNonQuery(strSQLSaveLog);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion
        //Function getAccount when charging card
        #region update Amount
        public void UpdateAmount(TransactionLog trangLog, Account acc, User user)
        {
            objData = new ConnectDB();
            UserModels usMd = new UserModels();

            string strSQLGetUserAmount = "";
            string strSQLUpdateAmount = "";
            double totalAmount = 0;
            double Bonus = 0;
            try
            {
                user.UserID = usMd.GetUserID(user.UserName);
                strSQLGetUserAmount = "sp_getTotalAmout @UserID =" + user.UserID + "";
                objData.BeginTrans();
                objData.ExeReader(strSQLGetUserAmount);
                try
                {
                    if (objData.dataRead.Read())
                    {
                        totalAmount = double.Parse(objData.dataRead["Money"].ToString());
                        Bonus = double.Parse(objData.dataRead["Bonus"].ToString());
                        acc.Money = totalAmount;
                        acc.Bonus = Bonus;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                // cộng số mệnh giá tiền nạp và tiền được cộng thêm vào tải khoản
                acc.Money += trangLog.Money;
                acc.Bonus += trangLog.Bonus;
                acc.UserID = user.UserID;
                strSQLUpdateAmount = "sp_InitAccount   @Money = " + acc.Money + ""
                                                + ",@Bonus =" + acc.Bonus + ""
                                                + ",@UserID=" + acc.UserID + ""
                                                + ",@GameID =" + acc.GameID + "" ;
                //Cập nhật số tiền vừa nạp vào tài khoản
                objData.ExeNonQuery(strSQLUpdateAmount);
                objData.CommitTrans();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                objData.RollBackTrans();
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion
        //Function getBonus
        #region GetBonus
        public double GetBonus(DateTime ChargingDate, string CardSupplierID) 
        {
            double bonus = 0;
            //Init data object
            objData = new ConnectDB();
            string strSQL = "";
            try 
            {
                strSQL = "sp_GetDiscount @ChargingDate='" + ChargingDate +"'"
                                     + ",@CardSupplierID ='" + CardSupplierID +"'";
                objData.ExeReader(strSQL);
                while(objData.dataRead.Read())
                {
                    bonus = double.Parse(objData.dataRead["Bonus"].ToString(), CultureInfo.InvariantCulture);
                }
                return bonus;
            }catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return bonus;
        }
        #endregion
    }
}